package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.pojo.entity.Admin;
import com.example.teachingSystem.pojo.entity.Teacher;
import com.example.teachingSystem.pojo.form.BasePageQuery;
import com.example.teachingSystem.pojo.form.admin.AddAdminForm;
import com.example.teachingSystem.pojo.form.admin.UpdateAdminForm;
import com.example.teachingSystem.pojo.vo.AdminInfoVo;
import com.example.teachingSystem.service.AdminService;
import com.example.teachingSystem.mapper.AdminMapper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 86147
 * @description 针对表【admin(管理员表)】的数据库操作Service实现
 * @createDate 2023-11-27 21:43:54
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
        implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result<String> addAdminInfo(AddAdminForm addAdminForm) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(addAdminForm, admin);
        Admin admin1 = adminMapper.selectById(admin);
        if (admin1 != null) {
            return Result.build(null, "添加失败，工号重复");
        }
        if (adminMapper.insert(admin) > 0) {
            return Result.ok("管理员信息录入成功");
        }
        return Result.build(null, "管理员信息录入失败");
    }

    @Override
    public Result<String> deleteAdminInfo(Long adminId) {
        Admin admin = adminMapper.selectById(adminId);
        if (admin == null) {
            return Result.build(null, "查无此人");
        }
        if (adminMapper.deleteById(admin) > 0) {
            return Result.ok("管理员信息删除成功");
        }
        return Result.build(null, "管理员信息删除失败");
    }

    @Override
    public Result<AdminInfoVo> getAdminById(Long adminId) {
        Admin admin = adminMapper.selectById(adminId);
        if (admin == null) {
            return Result.build(null, "查无此人");
        }
        AdminInfoVo adminInfoVo = new AdminInfoVo();
        BeanUtils.copyProperties(admin, adminInfoVo);
        return Result.ok(adminInfoVo);
    }

    @Override
    public PageResult<AdminInfoVo> getAllAdmin(BasePageQuery basePageQuery) {
        Page<AdminInfoVo> page = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
        IPage<AdminInfoVo> iPage = adminMapper.getAllAdminInfo(page);
        iPage.setTotal(iPage.getRecords().size());
        return PageResult.success(iPage);
    }

    @Override
    public Result<String> updatedmin(UpdateAdminForm updateAdminForm) {
        Admin admin = adminMapper.selectOne(new LambdaQueryWrapper<Admin>().
                eq(Admin::getAdminId,updateAdminForm.getAdminId()));
        if(admin==null){
            return Result.build(null,500,"不存在该管理员");
        }
        BeanUtils.copyProperties(updateAdminForm,admin);
        this.updateById(admin);
        return Result.ok(null,"修改成功");
    }
}




